angular.module('mamApp').controller('roleManageController', ['$scope', '$state', '$uibModal', 'SystemManager', 'ImsDialog', 'RoleManager', '$filter',
    function ($scope, $state, $uibModal, SystemManager, ImsDialog, RoleManager, $filter) {
        $scope.formData = {permission: {}};
        $scope.roleId = $state.params.roleId;
        if ($scope.roleId) {
            RoleManager.roleDetail($scope.roleId).then(function (result) {
                $scope.formData = result.data.adminRoleVO;
            })
        }
        /**
         * 组装页面显示的数据
         * @param channels
         * @param filed
         */

        function initChecked(channels, filed) {
            $scope.formData.permission[filed].forEach(function (reviewChannel) {
                channels.forEach(function (channel) {
                    if (reviewChannel.isAll && reviewChannel.id == channel.categoryId) {
                        channel.checked = 1;
                        if(channel.list){
                            channel.list.forEach(function (item) {
                                    item.checked = 1
                                }
                            )
                        }

                    } else {
                        if (channel.list) {
                            channel.list.forEach(function (item) {
                                if (reviewChannel.list.indexOf(item.channelId) > -1) {
                                    item.checked = 1;
                                }
                            })
                        }

                    }
                })
            })
        }


        $scope.itemChange = function (channel) {
            if (channel.list.length == $filter('filter')(channel.list, {checked: 1}).length) {
                channel.checked = 1;
            } else {
                channel.checked = 0;

            }
        }


        RoleManager.roleChannelList().then(function (result) {
            $scope.reviewChannels = result.data.list;
            $scope.editchannels = angular.copy(result.data.list);
            if ($scope.roleId) {
                RoleManager.roleDetail($scope.roleId).then(function (result) {
                    $scope.formData = result.data.adminRoleVO;
                    initChecked($scope.reviewChannels, 'review')
                    initChecked($scope.editchannels, 'edit')

                })
            }
        })


        $scope.channelAllChecked = function (channel) {
            if(channel.list){
                channel.list.forEach(function (item) {
                    item.checked = channel.checked;
                })
            }

        }

        /**
         * 组装保存的数据
         * @param channels
         * @returns {Array}
         */

        function concatReviewOrEditArray(channels) {
            var array = []
            if (channels) {
                channels.forEach(function (channel) {
                    var review = {id: channel.categoryId, name: channel.categoryName};
                    if (channel.checked) {
                        review.isAll = 1;
                    } else {
                        review.isAll = 0;
                    }
                    var ids = [];
                    if (channel.list && !review.isAll) {
                        channel.list.forEach(function (item) {
                            if (item.checked) {
                                ids.push(item.channelId)
                            }
                        })
                    }

                    review.list = ids;
                    array.push(review)
                })
            }

            return array;
        }


        $scope.submit = function () {
            $scope.formData.permission.review = concatReviewOrEditArray($scope.reviewChannels);
            $scope.formData.permission.edit = concatReviewOrEditArray($scope.editchannels);
            if ($scope.roleId) {
                RoleManager.roleEdit($scope.formData).then(function (result) {
                    if (result.data.c == "s") {
                        $state.go("main.role", {}, {reload: true});
                    }
                })
            } else {
                RoleManager.roleCreate($scope.formData).then(function (result) {
                    if (result.data.c == "s") {
                        $state.go("main.role", {}, {reload: true});
                    }
                })
            }

        }


    }])

